package com.example.xcpeekaboo.game;

import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class DisguiseManager {
    private static DisguiseManager instance;
    private final Map<Player, DisguiseType> playerDisguises;
    private final DisguiseType[] availableDisguises = {
        DisguiseType.COW,
        DisguiseType.PIG,
        DisguiseType.SHEEP,
        DisguiseType.CHICKEN,
        DisguiseType.MUSHROOM_COW,
        DisguiseType.RABBIT,
        DisguiseType.VILLAGER,
        DisguiseType.WOLF,
        DisguiseType.CAT,
        DisguiseType.PANDA,
        DisguiseType.BEE,
        DisguiseType.TURTLE,
        DisguiseType.FOX,
        DisguiseType.GOAT,
        DisguiseType.AXOLOTL,
        DisguiseType.SNOWMAN,
        DisguiseType.IRON_GOLEM,
        DisguiseType.ENDERMAN,
        DisguiseType.BLAZE,
        DisguiseType.SLIME,
        DisguiseType.MAGMA_CUBE,
        DisguiseType.BAT,
        DisguiseType.PARROT,
        DisguiseType.POLAR_BEAR,
        DisguiseType.DONKEY,
        DisguiseType.MULE,
        DisguiseType.CAMEL
    };

    private DisguiseManager() {
        this.playerDisguises = new HashMap<>();
    }

    public static DisguiseManager getInstance() {
        if (instance == null) {
            instance = new DisguiseManager();
        }
        return instance;
    }

    public void disguisePlayer(Player player) {
        DisguiseType disguiseType = getRandomDisguise();
        MobDisguise disguise = new MobDisguise(disguiseType);
        DisguiseAPI.disguiseEntity(player, disguise);
        playerDisguises.put(player, disguiseType);
    }

    public void disguisePlayer(Player player, DisguiseType disguiseType) {
        if (isValidDisguise(disguiseType)) {
            MobDisguise disguise = new MobDisguise(disguiseType);
            DisguiseAPI.disguiseEntity(player, disguise);
            playerDisguises.put(player, disguiseType);
        }
    }

    public void undisguisePlayer(Player player) {
        if (isDisguised(player)) {
            DisguiseAPI.undisguiseToAll(player);
            playerDisguises.remove(player);
        }
    }

    public boolean isDisguised(Player player) {
        return playerDisguises.containsKey(player);
    }

    public DisguiseType getCurrentDisguise(Player player) {
        return playerDisguises.get(player);
    }

    private DisguiseType getRandomDisguise() {
        Random random = new Random();
        return availableDisguises[random.nextInt(availableDisguises.length)];
    }

    private boolean isValidDisguise(DisguiseType disguiseType) {
        for (DisguiseType type : availableDisguises) {
            if (type == disguiseType) {
                return true;
            }
        }
        return false;
    }

    public void clearAllDisguises() {
        for (Player player : playerDisguises.keySet()) {
            undisguisePlayer(player);
        }
        playerDisguises.clear();
    }

    public void removeDisguise(Player player) {
        undisguisePlayer(player);
    }
}